/**
 * @title: DynaSqlProvider.java
 * @package com.wehealth.weiyu.common.data.mybatis
 * @description: TODO
 * @author hushsh
 * @date 2016年12月30日下午4:59:00
 * @version V1.0
 */

package com.apiserver.data.mybatis.sqlProvider;


import com.apiserver.data.idStrategy.IdProviderFactory;
import com.apiserver.data.mybatis.model.EntityTable;
import com.apiserver.data.mybatis.util.EntityUtil;
import com.apiserver.data.mybatis.util.MapTools;
import com.apiserver.kernel.utils.SnowFlake;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.ibatis.jdbc.SQL;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author feizhou
 * @Description 生成mybatis动态sql
 * @Date 下午 2:33 2019/10/13 0013
 * @Param
 * @return
 **/
public class Provider {


    /**
     * @return org.apache.ibatis.jdbc.SQL
     * @Author feizhou
     * @Description 返回where语句
     * @Date 下午 2:43 2019/10/13 0013
     * @Param [m, sql, entity]
     **/


    protected SQL getLikeWhere(Map<String, Object> likeQueryMap, SQL sql, EntityTable entity) {

        for (Map.Entry<String, Object> entry : likeQueryMap.entrySet()) {
            sql = sql.WHERE(entity.getPropertyColumn().get(entry.getKey()) + " like '%" + entry.getValue() + "%'");
        }
        return sql;
    }


    /**
     * @return org.apache.ibatis.jdbc.SQL
     * @Author feizhou
     * @Description 返回where语句
     * @Date 下午 2:43 2019/10/13 0013
     * @Param [m, sql, entity]
     **/


    protected <M> SQL getWhere(M m, SQL sql, EntityTable entity) {

        Map<String, Object> queryMap = MapTools.objectToMapNotNull(m);

        for (Map.Entry<String, Object> entry : queryMap.entrySet()) {
            sql = sql.WHERE(entity.getPropertyColumn().get(entry.getKey()) + "=#{m." + entry.getKey() + "}");
        }
        return sql;
    }

    /**
     * @return org.apache.ibatis.jdbc.SQL
     * @Author feizhou
     * @Description 返回where语句
     * @Date 下午 2:43 2019/10/13 0013
     * @Param [m, sql, entity]
     **/


    protected <M> SQL getWhere(Map<String, Object> queryMap, SQL sql, EntityTable entity) {

        for (Map.Entry<String, Object> entry : queryMap.entrySet()) {
            sql = sql.WHERE(entity.getPropertyColumn().get(entry.getKey()) + "=#{m." + entry.getKey() + "}");
        }
        return sql;
    }


    /**
     * @return java.lang.String
     * @Author feizhou
     * @Description 获取set 的语句
     * @Date 下午 9:47 2019/10/13 0013
     * @Param [m]
     **/

    protected <M> String getSetSql(M m, EntityTable entity) {
        String pkProperty = entity.getPKProperty();


        Map<String, Object> objectMap = MapTools.objectToMapNotNull(m);


        StringBuilder setSql = new StringBuilder();

        for (Map.Entry<String, Object> en : objectMap.entrySet()) {
            String key = en.getKey();
            //去掉主键
            if (pkProperty.equals(key)) {
                continue;
            }
            setSql.append(entity.getPropertyColumn().get(key) + "=#{m." + key + "} " + ",");
        }

        if (setSql.length() > 0) {
            return setSql.substring(0, setSql.length() - 1);
        }
        return null;
    }

    /**
     * @return
     * @Author feizhou
     * @Description insert 语句
     * @Date 下午 10:47 2019/10/13 0013
     * @Param
     **/

    protected <M> SQL getInSertSql(EntityTable entity, M m, SQL sql) {

        String pkProperty = entity.getPKProperty();
        Map<String, Object> objectMap = MapTools.objectToMapNotNull(m);

        if(objectMap.get(pkProperty)==null&&entity.getIdStrategy()!=null){
            if(IdProviderFactory.createIdProvider(entity.getIdStrategy()).toGenerateId(  entity,   m)){
                objectMap.put(pkProperty,pkProperty);
            }

        }




        for (String key : objectMap.keySet()) {
            sql = sql.VALUES(entity.getPropertyColumn().get(key), "#{" + key + "}");
        }
        return sql;
    }


    /**
     * @Author feizhou
     * @Description 创建主键的值
     * @Date 上午 1:03 2019/10/14 0014
     * @Param [entity]
     * @return void
     **/
    
    protected Object   createPKValue(EntityTable entity) {



        return 11L;

    }

    /**
     * @return java.lang.String
     * @Author feizhou
     * @Description 将集合转换成in 查询的字符串，如 返回 s="'1','2','3'"
     * @Date 下午 11:41 2019/10/11 0011
     * @Param [value]
     **/

    protected SQL getWhereForIn(List<Object> list, SQL sql, EntityTable entity, String queryName) {
        StringBuilder sb = new StringBuilder();
        for (Object o : list) {
            sb.append("'");
            sb.append(o);
            sb.append("'");
            sb.append(",");
        }

        if (sb.length() > 0) {
            sql = sql.WHERE(entity.getPropertyColumn().get(queryName) + " in (" + sb.substring(0, sb.length() - 1) + ")");
        }
        return sql;
    }

    /**
     * @return java.lang.String
     * @Author feizhou
     * @Description 将集合转换成in 查询的字符串，如 返回 s="'1','2','3'"
     * @Date 下午 11:41 2019/10/11 0011
     * @Param [value]
     **/
    protected SQL getWhereForIn(Map<String, List<Object>> arrayQueryMap, SQL sql, EntityTable entity) {

        for (Map.Entry<String, List<Object>> entry : arrayQueryMap.entrySet()) {
            getWhereForIn(entry.getValue(), sql, entity, entry.getKey());
        }

        return sql;
    }

    /**
     * @return org.apache.ibatis.jdbc.SQL
     * @Author feizhou
     * @Description 获取排序语句
     * @Date 下午 11:28 2019/10/13 0013
     * @Param [m, sql, entity]
     **/

    protected SQL getOrderSql(LinkedHashMap<String, String> sortMap, SQL sql, EntityTable entity) {

        for (Map.Entry<String, String> entry : sortMap.entrySet()) {
            sql.ORDER_BY(entity.getPropertyColumn().get(entry.getKey()) + " " + entry.getValue());
        }

        return sql;
    }

    /**
     * @return org.apache.ibatis.jdbc.SQL
     * @Author feizhou
     * @Description 分组sql
     * @Date 下午 11:39 2019/10/13 0013
     * @Param [groupList, sql]
     **/

    protected SQL getGroupSql(List<String> groupList, SQL sql) {
        for (String groupName : groupList) {
            sql = sql.GROUP_BY(groupName);
        }

        return sql;
    }

}
